package net.sx.minemodule.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ViewUtil;
import com.kedll.library.widget.MySwitchView;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineViewEntryLayoutBinding;

/**
 * @author 11470
 */
public class EntryLayoutView extends RelativeLayout {

    private MineViewEntryLayoutBinding mBinding;

    public EntryLayoutView(Context context) {
        this(context, null);
    }

    public EntryLayoutView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EntryLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.mine_view_entry_layout, this, true);
        mBinding.mySwitchView.setSwitchOpenColor(ContextCompat.getColor(context, R.color.color_a541c9));
    }

    public void setLeftTextAndRightImage(String text, boolean rightVisible) {
        if (mBinding.tvLeftContentEntry != null) {
            mBinding.tvLeftContentEntry.setVisibility(VISIBLE);
            mBinding.tvLeftContentEntry.setText(text);
        }

        if (mBinding.ivRightEntry != null) {
            mBinding.ivRightEntry.setVisibility(rightVisible ? VISIBLE : INVISIBLE);
        }

        if (mBinding.tvRightContentEntry != null) {
            mBinding.tvRightContentEntry.setVisibility(GONE);
        }

        if (mBinding.ivRightImgEntry != null) {
            mBinding.ivRightImgEntry.setVisibility(GONE);
        }

    }

    /**
     * 设置左部text和右步头像图片和图标显示
     *
     * @param text
     * @param imgUrl
     * @param rightVisible
     */
    public void setLeftTextAndRightImgRight(String text, String imgUrl, boolean rightVisible) {
        if (mBinding.tvLeftContentEntry != null) {
            mBinding.tvLeftContentEntry.setVisibility(VISIBLE);
            mBinding.tvLeftContentEntry.setText(text);
        }

        if (mBinding.ivRightEntry != null) {
            mBinding.ivRightEntry.setVisibility(rightVisible ? VISIBLE : INVISIBLE);
        }

        if (mBinding.tvRightContentEntry != null) {
            mBinding.tvRightContentEntry.setVisibility(GONE);
        }

        if (mBinding.ivRightImgEntry != null) {
            mBinding.ivRightImgEntry.setVisibility(VISIBLE);
            BindingUtil.loadCircleImage(mBinding.ivRightImgEntry, imgUrl, null);
        }
    }

    public void setRightImage(String imgUrl) {
        if (mBinding.ivRightImgEntry != null) {
            mBinding.ivRightImgEntry.setVisibility(VISIBLE);
            BindingUtil.loadCircleImage(mBinding.ivRightImgEntry, imgUrl, null);
        }
    }

    public void setLeftTextAndRightTextRightImg(String text, String rightText, boolean rightImgVis) {
        if (mBinding.tvLeftContentEntry != null) {
            mBinding.tvLeftContentEntry.setVisibility(VISIBLE);
            mBinding.tvLeftContentEntry.setText(text);
        }

        if (mBinding.ivRightImgEntry != null) {
            mBinding.ivRightImgEntry.setVisibility(GONE);
        }

        if (mBinding.ivRightEntry != null) {
            mBinding.ivRightEntry.setVisibility(rightImgVis ? VISIBLE : INVISIBLE);
        }

        if (mBinding.tvRightContentEntry != null) {
            mBinding.tvRightContentEntry.setVisibility(StringUtil.isNotEmpty(rightText) ? VISIBLE : INVISIBLE);
            mBinding.tvRightContentEntry.setText(rightText);
        }

    }

    public void leftTextAndVisible(String text, boolean isVisible) {
        if (mBinding.tvLeftContentEntry != null) {
            if (isVisible) {
                mBinding.tvLeftContentEntry.setVisibility(VISIBLE);
                mBinding.tvLeftContentEntry.setText(text);
            } else {
                mBinding.tvLeftContentEntry.setVisibility(INVISIBLE);
            }
        }
    }

    public void rightImageVisible(boolean isVisible) {
        if (mBinding.ivRightEntry != null) {
            if (isVisible) {
                mBinding.ivRightEntry.setVisibility(VISIBLE);
            } else {
                mBinding.ivRightEntry.setVisibility(GONE);
            }
        }
    }

    //add shibo.zheng start 19/06/13
    public void setRightTextColor(int color) {
        if (mBinding.tvRightContentEntry != null) {
            mBinding.tvRightContentEntry.setTextColor(color);
        }
    }
    //add shibo.zheng end 19/06/13

    public void rightTextAndVisible(String text, boolean isVisible) {
        if (mBinding.tvRightContentEntry != null) {
            if (isVisible) {
                mBinding.tvRightContentEntry.setVisibility(VISIBLE);
                mBinding.tvRightContentEntry.setText(text);
            } else {
                mBinding.tvRightContentEntry.setVisibility(INVISIBLE);
            }
        }
    }

    public void setLeftTextAndrRightSwitch(String leftText, boolean isVisSwitch) {
        if (mBinding.tvLeftContentEntry != null) {
            mBinding.tvLeftContentEntry.setVisibility(VISIBLE);
            mBinding.tvLeftContentEntry.setText(leftText);
        }

        if (mBinding.ivRightImgEntry != null) {
            mBinding.ivRightImgEntry.setVisibility(GONE);
        }

        if (mBinding.ivRightEntry != null) {
            mBinding.ivRightEntry.setVisibility(GONE);
        }

        if (mBinding.tvRightContentEntry != null) {
            mBinding.tvRightContentEntry.setVisibility(GONE);
        }

        if (mBinding.mySwitchView != null) {
            mBinding.mySwitchView.setVisibility(isVisSwitch ? VISIBLE : GONE);
//            mBinding.mySwitchView.setCircleColor;
        }

    }

    public void setSwitchCheck(boolean isCheck) {
        ViewUtil.setGone(mBinding.mySwitchView, false);
        mBinding.mySwitchView.setCheck(isCheck);
    }

    public void setOnSwitchCheckChangeListener(MySwitchView.OnCheckListener l) {
        ViewUtil.setGone(mBinding.mySwitchView, false);
        mBinding.mySwitchView.setOnCheckListener(l);
    }


}
